let fs = require('fs')
let FXdata = JSON.parse(fs.readFileSync('./fxHTML/FXdata.json'))
// .replace('\\', '').replace("'", '"')

const cheerio = require('cheerio');
var Entities = require('html-entities').XmlEntities;
entities = new Entities();

let fx_arr = []
let pd_map = {}
FXdata.forEach(data => {
    let ele = data
    const $ = cheerio.load(ele.FXdata.replace('\\', '').replace("'", '"'))

    var reg = new RegExp(/[機體血量敏捷性速度\s必殺技鎖敵距離推進時間\r\n\t]/, "g")
    let ID = $("img").attr("src") && $("img").attr("src").match(/\d+/) ? $("img").attr("src").match(/\d+/)[0] : ""
    let HP = $($("#machine_1 ul")[4]).text().match('血量') ? $($("#machine_1 ul")[4]).text().replace(reg, "").trim() : ""
    let agility1 = $($("#machine_1 ul")[8]).text().match('敏捷') ? $($("#machine_1 ul")[8]).text().replace(reg, "").trim() : ""
    let agility2 = $($("#machine_2 ul")[3]).text() ? $($("#machine_2 ul")[3]).text().replace(reg, "").trim() : ""
    let speed1 = $($("#machine_1 ul")[9]).text().match('速') ? $($("#machine_1 ul")[9]).text().replace(reg, "").trim() : ""
    let speed2 = $($("#machine_2 ul")[4]).text() ? $($("#machine_2 ul")[4]).text().replace(reg, "").trim() : ""
    let sp1_p = $($("#machine_1 ul")[10]).text().match('必') ? $($("#machine_1 ul")[10]).text().replace(reg, "").trim() : ""
    let sp2_p = $($("#machine_2 ul")[5]).text() ? $($("#machine_2 ul")[5]).text().replace(reg, "").trim() : ""
    let dect1 = $($("#machine_1 ul")[11]).text().match('鎖') ? $($("#machine_1 ul")[11]).text().replace(reg, "").trim() : ""
    let dect2 = $($("#machine_2 ul")[6]).text() ? $($("#machine_2 ul")[6]).text().replace(reg, "").trim() : ""
    let boost_time1 = $($("#machine_1 ul")[12]).text().match('推進時間') ? $($("#machine_1 ul")[12]).text().replace(reg, "").trim() : ""
    let boost_time2 = $($("#machine_2 ul")[7]).text() ? $($("#machine_2 ul")[7]).text().replace(reg, "").trim() : ""

    let reg_w = new RegExp(/[係數射程彈數回彈冷卻倒地\:\：\s]/, "g")
    let arm1_1_name = $($($("#weapon ul")[0]).find('li')[1]).text() ? $($($("#weapon ul")[0]).find('li')[1]).text().trim() : ""
    let arm1_1_type = $($($("#weapon ul")[0]).find('li')[2]).text().split("/")[0] ? $($($("#weapon ul")[0]).find('li')[2]).text().split("/")[0].trim().replace("格鬥", "格斗").replace("實彈", "实弹") : ""
    let arm1_1_pd = $($($("#weapon ul")[0]).find('li')[2]).text().split("/")[1] ? $($($("#weapon ul")[0]).find('li')[2]).text().split("/")[1].trim().replace("近距離", "近").replace("中距離", "中").replace("遠距離", "远").replace("砲", "浮游炮") : ""
    let arm1_1_des = ""
    let arm1_1_power = ""
    let arm1_1_ping = ""
    let arm1_1_ammunitition = ""
    let arm1_1_dfloor = ""
    let arm1_1_reload = ""
    let arm1_1_arr = $($($("#weapon ul")[0]).find('.w4')).length ? $($($("#weapon ul")[0]).find('.w4')).html().split("<br>") : []
    arm1_1_arr.forEach(ele => {
        if (entities.decode(ele) && entities.decode(ele).match("係數")) {
            arm1_1_power = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("射程")) {
            arm1_1_des = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("彈數")) {
            arm1_1_ammunitition = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("回彈")) {
            arm1_1_reload = entities.decode(ele).replace(reg_w, "").replace('發', "发").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("冷卻")) {
            arm1_1_ping = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("倒地")) {
            arm1_1_dfloor = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
    });

    let arm1_2_name = $($($("#weapon ul")[1]).find('li')[1]).text() ? $($($("#weapon ul")[1]).find('li')[1]).text().trim() : ""
    let arm1_2_type = $($($("#weapon ul")[1]).find('li')[2]).text().split("/")[0] ? $($($("#weapon ul")[1]).find('li')[2]).text().split("/")[0].trim().replace("格鬥", "格斗").replace("實彈", "实弹") : ""
    let arm1_2_pd = $($($("#weapon ul")[1]).find('li')[2]).text().split("/")[1] ? $($($("#weapon ul")[1]).find('li')[2]).text().split("/")[1].trim().replace("近距離", "近").replace("中距離", "中").replace("遠距離", "远").replace("砲", "浮游炮") : ""
    let arm1_2_des = ""
    let arm1_2_power = ""
    let arm1_2_ping = ""
    let arm1_2_ammunitition = ""
    let arm1_2_dfloor = ""
    let arm1_2_reload = ""
    let arm1_2_arr = $($($("#weapon ul")[1]).find('.w4')).length ? $($($("#weapon ul")[1]).find('.w4')).html().split("<br>") : []
    arm1_2_arr.forEach(ele => {
        if (entities.decode(ele) && entities.decode(ele).match("係數")) {
            arm1_2_power = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("射程")) {
            arm1_2_des = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("彈數")) {
            arm1_2_ammunitition = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("回彈")) {
            arm1_2_reload = entities.decode(ele).replace(reg_w, "").replace('發', "发").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("冷卻")) {
            arm1_2_ping = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("倒地")) {
            arm1_2_dfloor = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
    });

    let arm1_3_name = $($($("#weapon ul")[2]).find('li')[1]).text() ? $($($("#weapon ul")[2]).find('li')[1]).text().trim() : ""
    let arm1_3_type = $($($("#weapon ul")[2]).find('li')[2]).text().split("/")[0] ? $($($("#weapon ul")[2]).find('li')[2]).text().split("/")[0].trim().replace("格鬥", "格斗").replace("實彈", "实弹") : ""
    let arm1_3_pd = $($($("#weapon ul")[2]).find('li')[2]).text().split("/")[1] ? $($($("#weapon ul")[2]).find('li')[2]).text().split("/")[1].trim().replace("近距離", "近").replace("中距離", "中").replace("遠距離", "远").replace("砲", "浮游炮") : ""
    let arm1_3_des = ""
    let arm1_3_power = ""
    let arm1_3_ping = ""
    let arm1_3_ammunitition = ""
    let arm1_3_dfloor = ""
    let arm1_3_reload = ""
    let arm1_3_arr = $($($("#weapon ul")[2]).find('.w4')).length ? $($($("#weapon ul")[2]).find('.w4')).html().split("<br>") : []
    arm1_3_arr.forEach(ele => {
        if (entities.decode(ele) && entities.decode(ele).match("係數")) {
            arm1_3_power = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("射程")) {
            arm1_3_des = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("彈數")) {
            arm1_3_ammunitition = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("回彈")) {
            arm1_3_reload = entities.decode(ele).replace(reg_w, "").replace('發', "发").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("冷卻")) {
            arm1_3_ping = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("倒地")) {
            arm1_3_dfloor = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
    });

    let arm2_1_name = $($($("#weapon ul")[3]).find('li')[1]).text() ? $($($("#weapon ul")[3]).find('li')[1]).text().trim() : ""
    let arm2_1_type = $($($("#weapon ul")[3]).find('li')[2]).text().split("/")[0] ? $($($("#weapon ul")[3]).find('li')[2]).text().split("/")[0].trim().replace("格鬥", "格斗").replace("實彈", "实弹") : ""
    let arm2_1_pd = $($($("#weapon ul")[3]).find('li')[2]).text().split("/")[1] ? $($($("#weapon ul")[3]).find('li')[2]).text().split("/")[1].trim().replace("近距離", "近").replace("中距離", "中").replace("遠距離", "远").replace("砲", "浮游炮") : ""
    let arm2_1_des = ""
    let arm2_1_power = ""
    let arm2_1_ping = ""
    let arm2_1_ammunitition = ""
    let arm2_1_dfloor = ""
    let arm2_1_reload = ""
    let arm2_1_arr = $($($("#weapon ul")[3]).find('.w4')).length ? $($($("#weapon ul")[3]).find('.w4')).html().split("<br>") : []
    arm2_1_arr.forEach(ele => {
        if (entities.decode(ele) && entities.decode(ele).match("係數")) {
            arm2_1_power = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("射程")) {
            arm2_1_des = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("彈數")) {
            arm2_1_ammunitition = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("回彈")) {
            arm2_1_reload = entities.decode(ele).replace(reg_w, "").replace('發', "发").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("冷卻")) {
            arm2_1_ping = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("倒地")) {
            arm2_1_dfloor = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
    });

    let arm2_2_name = $($($("#weapon ul")[4]).find('li')[1]).text() ? $($($("#weapon ul")[4]).find('li')[1]).text().trim() : ""
    let arm2_2_type = $($($("#weapon ul")[4]).find('li')[2]).text().split("/")[0] ? $($($("#weapon ul")[4]).find('li')[2]).text().split("/")[0].trim().replace("格鬥", "格斗").replace("實彈", "实弹") : ""
    let arm2_2_pd = $($($("#weapon ul")[4]).find('li')[2]).text().split("/")[1] ? $($($("#weapon ul")[4]).find('li')[2]).text().split("/")[1].trim().replace("近距離", "近").replace("中距離", "中").replace("遠距離", "远").replace("砲", "浮游炮") : ""
    let arm2_2_des = ""
    let arm2_2_power = ""
    let arm2_2_ping = ""
    let arm2_2_ammunitition = ""
    let arm2_2_dfloor = ""
    let arm2_2_reload = ""
    let arm2_2_arr = $($($("#weapon ul")[4]).find('.w4')).length ? $($($("#weapon ul")[4]).find('.w4')).html().split("<br>") : []
    arm2_2_arr.forEach(ele => {
        if (entities.decode(ele) && entities.decode(ele).match("係數")) {
            arm2_2_power = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("射程")) {
            arm2_2_des = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("彈數")) {
            arm2_2_ammunitition = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("回彈")) {
            arm2_2_reload = entities.decode(ele).replace(reg_w, "").replace('發', "发").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("冷卻")) {
            arm2_2_ping = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("倒地")) {
            arm2_2_dfloor = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
    });

    let arm2_3_name = $($($("#weapon ul")[5]).find('li')[1]).text() ? $($($("#weapon ul")[5]).find('li')[1]).text().trim() : ""
    let arm2_3_type = $($($("#weapon ul")[5]).find('li')[2]).text().split("/")[0] ? $($($("#weapon ul")[5]).find('li')[2]).text().split("/")[0].trim().replace("格鬥", "格斗").replace("實彈", "实弹") : ""
    let arm2_3_pd = $($($("#weapon ul")[5]).find('li')[2]).text().split("/")[1] ? $($($("#weapon ul")[5]).find('li')[2]).text().split("/")[1].trim().replace("近距離", "近").replace("中距離", "中").replace("遠距離", "远").replace("砲", "浮游炮") : ""
    let arm2_3_des = ""
    let arm2_3_power = ""
    let arm2_3_ping = ""
    let arm2_3_ammunitition = ""
    let arm2_3_dfloor = ""
    let arm2_3_reload = ""
    let arm2_3_arr = $($($("#weapon ul")[5]).find('.w4')).length ? $($($("#weapon ul")[5]).find('.w4')).html().split("<br>") : []
    arm2_3_arr.forEach(ele => {
        if (entities.decode(ele) && entities.decode(ele).match("係數")) {
            arm2_3_power = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("射程")) {
            arm2_3_des = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("彈數")) {
            arm2_3_ammunitition = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("回彈")) {
            arm2_3_reload = entities.decode(ele).replace(reg_w, "").replace('發', "发").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("冷卻")) {
            arm2_3_ping = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
        if (entities.decode(ele) && entities.decode(ele).match("倒地")) {
            arm2_3_dfloor = entities.decode(ele).replace(reg_w, "").trim() || ""
        }
    });

    let gundam = {
        ID: ID,
        HP: HP,
        agility1: agility1,
        agility2: agility2,
        speed1: speed1,
        speed2: speed2,
        sp1_p: sp1_p,
        sp2_p: sp2_p,
        dect1: dect1,
        dect2: dect2,
        boost_time1: boost_time1,
        boost_time2: boost_time2,
        arm1_1_name: arm1_1_name,
        arm1_1_type: arm1_1_type,
        arm1_1_pd: arm1_1_pd,
        arm1_1_des: arm1_1_des,
        arm1_1_power: arm1_1_power,
        arm1_1_ping: arm1_1_ping,
        arm1_1_ammunitition: arm1_1_ammunitition,
        arm1_1_dfloor: arm1_1_dfloor,
        arm1_1_reload: arm1_1_reload,


        arm1_2_name: arm1_2_name,
        arm1_2_type: arm1_2_type,
        arm1_2_pd: arm1_2_pd,
        arm1_2_des: arm1_2_des,
        arm1_2_power: arm1_2_power,
        arm1_2_ping: arm1_2_ping,
        arm1_2_ammunitition: arm1_2_ammunitition,
        arm1_2_dfloor: arm1_2_dfloor,
        arm1_2_reload: arm1_2_reload,

        arm1_3_name: arm1_3_name,
        arm1_3_type: arm1_3_type,
        arm1_3_pd: arm1_3_pd,
        arm1_3_des: arm1_3_des,
        arm1_3_power: arm1_3_power,
        arm1_3_ping: arm1_3_ping,
        arm1_3_ammunitition: arm1_3_ammunitition,
        arm1_3_dfloor: arm1_3_dfloor,
        arm1_3_reload: arm1_3_reload,

        arm2_1_name: arm2_1_name,
        arm2_1_type: arm2_1_type,
        arm2_1_pd: arm2_1_pd,
        arm2_1_des: arm2_1_des,
        arm2_1_power: arm2_1_power,
        arm2_1_ping: arm2_1_ping,
        arm2_1_ammunitition: arm2_1_ammunitition,
        arm2_1_dfloor: arm2_1_dfloor,
        arm2_1_reload: arm2_1_reload,

        arm2_2_name: arm2_2_name,
        arm2_2_type: arm2_2_type,
        arm2_2_pd: arm2_2_pd,
        arm2_2_des: arm2_2_des,
        arm2_2_power: arm2_2_power,
        arm2_2_ping: arm2_2_ping,
        arm2_2_ammunitition: arm2_2_ammunitition,
        arm2_2_dfloor: arm2_2_dfloor,
        arm2_2_reload: arm2_2_reload,

        arm2_3_name: arm2_3_name,
        arm2_3_type: arm2_3_type,
        arm2_3_pd: arm2_3_pd,
        arm2_3_des: arm2_3_des,
        arm2_3_power: arm2_3_power,
        arm2_3_ping: arm2_3_ping,
        arm2_3_ammunitition: arm2_3_ammunitition,
        arm2_3_dfloor: arm2_3_dfloor,
        arm2_3_reload: arm2_3_reload,
    }

    if (!pd_map[arm1_1_pd])
        pd_map[arm1_1_pd] = true
    if (!pd_map[arm1_2_pd])
        pd_map[arm1_2_pd] = true
    if (!pd_map[arm1_3_pd])
        pd_map[arm1_3_pd] = true
    if (!pd_map[arm2_1_pd])
        pd_map[arm2_1_pd] = true
    if (!pd_map[arm2_2_pd])
        pd_map[arm2_2_pd] = true
    if (!pd_map[arm2_3_pd])
        pd_map[arm2_3_pd] = true

    fx_arr.push(gundam)

});
let str2 = JSON.stringify(fx_arr)

fs.writeFileSync(__dirname + '/fxArr.json', str2)
fs.writeFileSync(__dirname + '/pdmap.json', JSON.stringify(pd_map))